﻿using AutoMapper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;

namespace leapt.AutoMapperConfig
{
    /// <summary>
    /// AutoMapper扩展
    /// </summary>
    public static class MappingExtensions
    {
        /// <summary>
        ///  类型映射
        /// </summary>
        public static T MapTo<T>(this object obj)
        {
            if (obj == null) return default(T);

            return AutoMapperConfiguration.Mapper.Map<T>(obj);
        }

        /// <summary>
        /// 类型映射
        /// </summary>
        public static TDestination MapTo<TSource, TDestination>(this TSource source, TDestination destination)
                    where TSource : class
                    where TDestination : class
        {
            if (source == null) return destination;
            return AutoMapperConfiguration.Mapper.Map(source, destination);
        }

        /// <summary>
        /// 集合列表类型映射
        /// </summary>
        public static List<TDestination> MapToList<TDestination>(this IEnumerable source)
        {
            return AutoMapperConfiguration.Mapper.Map<List<TDestination>>(source);
        }

        /// <summary>
        /// 集合列表类型映射
        /// </summary>
        public static List<TDestination> MapToList<TSource, TDestination>(this IEnumerable<TSource> source)
        {
            return AutoMapperConfiguration.Mapper.Map<List<TDestination>>(source);
        }
        
        /// <summary>
        /// DataReader映射
        /// </summary>
        public static IEnumerable<T> DataReaderMapTo<T>(this IDataReader reader)
        {
            return AutoMapperConfiguration.Mapper.Map<IDataReader, IEnumerable<T>>(reader);
        }
        
        #region User

        //public static UserViewModel ToModel(this User entity)
        //{
        //    return entity.MapTo<User, UserViewModel>();
        //}

        //public static User ToEntity(this UserViewModel model)
        //{
        //    return model.MapTo<UserViewModel, User>();
        //}

        #endregion

    }
}
